import tensorflow as tf
import os
import numpy as np
import cv2

np.set_printoptions(threshold=np.inf)

# mnist = tf.keras.datasets.mnist
# (x_train, y_train), (x_test, y_test) = mnist.load_data()
# x_train, x_test = x_train / 255.0, x_test / 255.0
#
# print(x_train.shape)
# print(type(x_train[0]))
# print(type(y_train))
# print(type(x_test))
# print(type(y_test))
# print(y_train.shape)
# 训练集图片所在的文件夹，以训练集为例，文件的命名为“类别_顺序.jpg”
train_path = "train_image/"

# 这个函数用于返回符合,可以使用正则路径，*表示任意字符
# path_list = tf.data.Dataset.list_files(train_path + "**/*.*g")


# # 定义一个读取图片的函数
# def read_image(path_list):
#     '''
#     :param path_list:文件的路径list
#     :return: 图片张量列表,图片标签列表
#     '''
#     images = []  # 图片聊表
#     image_labels = []  # 图片标签列表
#
#     # 根据文件路径列表依次读取
#     for i in path_list:
#         # image_temp = tf.io.read_file(i)  # tesnsorflow的io读取文件
#         # image_temp = tf.image.decode_jpeg(image_temp, channels=3)  # 根据图片的格式进行编码转化为张量，这里图片是jpg格式
#         # image_temp = tf.cast(image_temp, dtype=tf.float16)  # 根据图片的格式进行编码转化为张量，这里图片是jpg格式
#         # print(i)
#         image_temp = cv2.imread('/Users/bink/PycharmProjects/nn-dl/CNN/DogCatFish/train_image/0/000.jpg')
#         image = tf.image.resize_with_crop_or_pad(image_temp, 28, 28)
#         image = tf.image.per_image_standardization(image)
#         images.append(image)  # 图片加入到数据集
#         image_labels.append(str(i).split('/')[1])  # 获取文件名加入到标签，这里要张量i转化为字符串
#
#     return np.array(images), np.array(image_labels)
#
#
# # 读取训练图片
# train_images, train_labels = read_image(path_list=path_list)
# print(train_images.shape)
# print(train_images[0])
# print(type(train_images[0]))
# print(type(train_labels))
# print(train_labels.shape)
#
# print(train_labels)


# print(cv2.imread('/Users/bink/PycharmProjects/nn-dl/CNN/DogCatFish/train_image/0/000.jpg'))


def get_files(images_path):
    file_list = []
    for root, dirs, files in os.walk(images_path):
        # for dir in dirs:
        # print(os.path.join(root,dir))
        for file in files:
            if file.endswith('g'):
                file_list.append(os.path.join(root, file))
    return file_list


def get_images(path):
    image_list = []
    label_list = []
    path_list = get_files(images_path=path)
    for path in path_list:
        image = cv2.imread(path)
        image_list.append(image)
        label_list.append(path.split('/')[1])
    return image_list, label_list


get_images(train_path)